package cc.dullsky.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpUtil {
	public static final String REGEX_HTML = "<(\\w+)([^<>]*)>([^<>]+)</\\1>";
	public static final String REGEX_LINK = "[?,&]id=[0-9]+";

	/**
	 * 过滤html标签，获取html标签内容
	 * 
	 * @param htmlString
	 * @return String
	 */
	public static String getTextFormHtml(String htmlString) {
		if (htmlString == null) {
			return "";
		}
		Pattern pattern = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		Matcher matcher = pattern.matcher(htmlString);
		StringBuilder result = new StringBuilder();
		while (matcher.find()) {
			result.append(matcher.group(3));
		}
		return result.toString();
	}

	/**
	 * 过滤链接，获取其中的id=后面的数字，如a.com/aa?aa=abc&id=123456&bb=bcd中的123456
	 * 
	 * @param link
	 * @return id
	 */
	public static String getIdFromLink(String link) {
		Pattern pattern = Pattern.compile(REGEX_LINK, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		Matcher matcher = pattern.matcher(link);
		String result = null;
		while (matcher.find()) {
			result = matcher.group();
		}
		return result == null ? null : result.substring(4);
	}

	public static void main(String[] args) {
		String htmlString = "http://music.163.com/#/song?id=33206214";
		System.out.println(getIdFromLink(htmlString));
	}
}
